package com.jd.ty.service;

import java.util.ArrayList;
import java.util.List;

import com.jd.ty.dao.constant.QueryConstants;
import com.jd.ty.dao.util.HqlCriterions;
import com.jd.ty.dao.FunctionDAO;
import com.jd.ty.pojo.Function;

public class FunctionService {

	private FunctionDAO functionDAO;

    @SuppressWarnings("unchecked")
	public List<Function> getList() {
    	List<Function> funcs = functionDAO.getByQuery("");
    	for (Function func : funcs) {
    		for (Function children : func.getChildren()) {
    			functionDAO.initialize(children);
    		}
    	}
    	
    	return funcs;
    }
    
    @SuppressWarnings("unchecked")
	public List<Function> getTree() {
    	HqlCriterions hc = new HqlCriterions();
    	hc.and(QueryConstants.Function.PARENT, null, QueryConstants.EQ);
    	List<Function> res = functionDAO.getByQuery(hc.getHqlString());
    	for (Function func : res) {
    		for (Function children : func.getChildren()) {
    			functionDAO.initialize(children);
    		}
    	}
    	return res;
    }
    @SuppressWarnings("unchecked")
	public List<Function> getSubTree() {
    	HqlCriterions hc = new HqlCriterions();
    	hc.and(QueryConstants.Function.PARENT, null, QueryConstants.EQ);
    	List<Function> res = functionDAO.getByQuery(hc.getHqlString());
    	List<Function> funs=new ArrayList<Function>();
    	for (Function func : res) {
    		for (Function children : func.getChildren()) {
    			functionDAO.initialize(children);
    			funs.add(children);
    		}
    	}
    	return funs;
    }
    public Function load(long id) {
    	Function func = (Function) functionDAO.loadById(id);
    	for (Function children : func.getChildren()) {
			functionDAO.initialize(children);
		}
    	
    	return func;
    }

	public void setFunctionDAO(FunctionDAO functionDAO) {
		this.functionDAO = functionDAO;
	}
	
}
